package com.doge.walknovel.business.home.genres

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.setPadding
import androidx.fragment.app.Fragment
import com.doge.wnpro.R
import com.doge.walknovel.business.home.genres.genres.GenresTabFragment
import com.doge.walknovel.business.home.genres.rank.RankTabFragment
import com.doge.walknovel.business.home.profile.topup.TopUpActivity
import com.doge.walknovel.business.home.profile.topup.TopUpRefer
import com.doge.walknovel.business.search.SearchActivity
import com.doge.walknovel.core.BaseFragment
import com.doge.wnpro.databinding.FragmentGenresBinding
import com.doge.walknovel.utils.setOnClickCallback
import com.doge.walknovel.utils.setupWithViewPager2
import com.doge.walknovel.utils.systemWindowInsetsTop

class GenresFragment : BaseFragment() {
    private var innerBinding: FragmentGenresBinding? = null
    private val binding get() = innerBinding!!
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding =
            innerBinding ?: FragmentGenresBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        systemWindowInsetsTop.observe(viewLifecycleOwner) {
            binding.fitsSys.setPadding(it / 2)
        }
        binding.search.setOnClickCallback { SearchActivity.start(it.context) }
        binding.topUp.setOnClickCallback { TopUpActivity.start(it.context, TopUpRefer.genres) }
        // setupViewPager delayed
        handler.sendEmptyMessageDelayed(msgDelayInitialTask, 2000)
    }

    private val msgDelayInitialTask = 101
    private val handler = Handler(Looper.getMainLooper()) {
        if (it.what == msgDelayInitialTask) {
            it.target.removeMessages(msgDelayInitialTask)
            setupViewPager()
        }
        true
    }

    private fun setupViewPager() {
        binding.viewPager.offscreenPageLimit = 2
        binding.viewPager.adapter = object : com.doge.walknovel.utils.FragmentStateAdapter(this) {
            private val tabs by lazy {
                listOf(R.string.rank, R.string.genres)
            }

            override fun getTitle(position: Int): String {
                return getString(tabs[position])
            }

            override fun getItemCount(): Int = tabs.size

            override fun createFragment(position: Int): Fragment {
                return when (position) {
                    0 -> RankTabFragment.newInstance()
                    1 -> GenresTabFragment.newInstance()
                    else -> BaseFragment()
                }
            }
        }
        binding.tab.setupWithViewPager2(binding.viewPager)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        handler.removeMessages(msgDelayInitialTask)
        binding.viewPager.adapter = null
        binding.search.setOnClickListener(null)
        binding.topUp.setOnClickListener(null)
        innerBinding = null
    }

}